package com.erp.module.xianChangPaiZhao

import androidx.appcompat.app.AlertDialog
import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.AdapterView
import com.erp.Interface.OnUserDataInterface
import com.erp.R
import com.erp.activity.BaseActivity
import com.erp.activity.DanjianivActivity
import com.erp.bean.*
import com.erp.myAdapter.PhotoAdapter
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.activity_anzhuang_photo.*
import kotlinx.android.synthetic.main.dialog_photo_long.*
import kotlinx.android.synthetic.main.header_knoya.*
import org.json.JSONException
import org.json.JSONObject

/**
 * Created by Administrator on 2019/3/22/022.
 * 作者：刘洋
 * 安装管理的照片以及问题信息
 */
class XianchangPaizhaoActivity : BaseActivity(), AdapterView.OnItemLongClickListener, AdapterView.OnItemClickListener, View.OnClickListener {
    lateinit var adapter: PhotoAdapter
    private var oid =""
    var onum = ""
    lateinit var AnZhuangBean : AnZhuangphotoBean
    lateinit var updatedialog: AlertDialog
    var code = 0
    private val a = "DanjuWTActivity"
    var model = XianChangPaiZhapModel()
    override fun getLayoutId(): Int {
        activityname = a
        return R.layout.activity_anzhuang_photo
    }
    override fun initCreate() {
        iv_header_left.visibility = View.VISIBLE
        oid = intent.getStringExtra("oid")
        code = intent.getIntExtra("code", code)
        onum = intent.getStringExtra("onum")
        if(onum == ""){
            tv_header_title.text = "安装问题拍照"
        }else{
            tv_header_title.text = onum
        }
        AnZhuangBean = AnZhuangphotoBean()
        adapter = PhotoAdapter(this@XianchangPaizhaoActivity,AnZhuangBean.listphoto,true)
        GvAnZHuangPhoto.adapter = adapter

        Log.d("onClick","code="+code)
        iv_header_left.setOnClickListener(this)
        tv_header_right.setOnClickListener(this)
        BtAnZhuangShangChuan.setOnClickListener(this)
        GvAnZHuangPhoto.onItemClickListener = this
        GvAnZHuangPhoto.onItemLongClickListener = this
        getHttpPhoto()
        if(code == 2){
            Myutil.photo(this@XianchangPaizhaoActivity, null, 1)
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            iv_header_left -> finish()
            tv_header_right -> {
//                adapter.listcb.clear()
//                adapter.notifyDataSetChanged()
//                rl_herder.visibility = View.GONE
//                val intent = Intent(this@LingShi2Activity, WtPhotoActivity::class.java)
//                intent.putExtra("oid", oid)
//                intent.putExtra("code", code)
//                startActivityForResult(intent, 1)
            }
            BtAnZhuangShangChuan -> {
                updatedialog = Myutil.showupdatedialog(this@XianchangPaizhaoActivity, "正在上传安装数据", false)
                AnZhuangBean.isl_asp = EtIslAsp.text.toString()
                model.setAnZhuang(this@XianchangPaizhaoActivity,AnZhuangBean,oid,object : OnUserDataInterface{
                    override fun onUserData(data: String?) {
                        if(data == null){
                            Myutil.MyTost("上传失败")
                            updatedialog.dismiss()
                            return
                        }
                        Myutil.MyTost("上传成功")
                        updatedialog.dismiss()
                        jsondata(data)
                        adapter.Allnotify(AnZhuangBean.listphoto)
                        Myutil.setet(AnZhuangBean.isl_asp,EtIslAsp)
                        Log.d("AnZhuangBean","AnZhuangBean="+AnZhuangBean.toString())
                    }

                    override fun onNot(throwable: String?) {
                        Log.d("getHttpClient","error"+throwable)
                        Myutil.MyTost("上传失败")
                        updatedialog.dismiss()
                    }

                })
            }
        }
    }

    fun getHttpPhoto() {
        updatedialog = Myutil.showupdatedialog(this@XianchangPaizhaoActivity, "获取安装信息", false)
        model.getAnZhuangXingxi(this, oid, object : OnUserDataInterface {
            override fun onUserData(data: String) {
                Log.d("getHttpClient","date"+data)
                updatedialog.dismiss()
                jsondata(data)
                adapter.Allnotify(AnZhuangBean.listphoto)
                Myutil.setet(AnZhuangBean.isl_asp,EtIslAsp)
                Log.d("AnZhuangBean","AnZhuangBean="+AnZhuangBean.toString())
            }

            override fun onNot(throwable: String) {
                Log.d("getHttpClient","error"+throwable)
                updatedialog.dismiss()
            }
        })
    }

    /**
     * 解析数据
     */
    private fun  jsondata(data: String) {
        Myutil.longlogshow("getHttpClient","data="+data)
        AnZhuangBean = AnZhuangphotoBean()
        try {
            val jo = JSONObject(data)
            AnZhuangBean.isl_asp = JsonUtil.getvolue(jo, "isl_asp")
            val js = JsonUtil.getjsvolue(jo, "isl_attachment")
            if (js != null) {
                for (i in 0 until js.length()) {
                    val jo1 = js.get(i) as JSONObject
                    if (jo1.getString("filemime").indexOf("image") != -1) {
                        val bean = PhotoBean()
                        bean.fid = JsonUtil.getvolue(jo1, "fid")
                        bean.uri = JsonUtil.getvolue(jo1, "uri")
                        bean.filename = JsonUtil.getvolue(jo1, "filename")
                        bean.timestamp = JsonUtil.getvolue(jo1, "timestamp")
                        bean.filemime = JsonUtil.getvolue(jo1, "filemime")
                        AnZhuangBean.listphoto.add(bean)
                    }
                }
            }

        } catch (e: JSONException) {
            e.printStackTrace()
        }

    }

    override fun onItemLongClick(parent: AdapterView<*>, view: View, position: Int, id: Long): Boolean {
        if(position < AnZhuangBean.listphoto.size){
                val quitdialog = AlertDialog.Builder(this@XianchangPaizhaoActivity).create()
                quitdialog.show()
                quitdialog.window!!.setContentView(R.layout.dialog_photo_long)
                quitdialog.tv_title.text = "确定要删除这张图片吗？"
                quitdialog.setCanceledOnTouchOutside(false)
                quitdialog.tv_quite_cancel.setOnClickListener { quitdialog.dismiss() }
                quitdialog.tv_quite_confirm.setOnClickListener {
                    if(AnZhuangBean.listphoto[position].fid != ""){
                        updatedialog = Myutil.showupdatedialog(this@XianchangPaizhaoActivity, "正在删除照片", false)
                        model.deleteAnZhuang(this@XianchangPaizhaoActivity,oid,AnZhuangBean.listphoto[position],object : OnUserDataInterface{
                            override fun onUserData(data: String?) {
                                if(data == null){
                                    Myutil.MyTost("删除失败")
                                    updatedialog.dismiss()
                                    return
                                }
                                Myutil.MyTost("删除照片成功")
                                updatedialog.dismiss()
                                jsondata(data)
                                adapter.Allnotify(AnZhuangBean.listphoto)
                                Myutil.setet(AnZhuangBean.isl_asp,EtIslAsp)
                                Log.d("AnZhuangBean","AnZhuangBean="+AnZhuangBean.toString())
                            }

                            override fun onNot(throwable: String?) {
                                Log.d("getHttpClient","error"+throwable)
                                Myutil.MyTost("删除失败")
                                updatedialog.dismiss()
                            }

                        })

                    }
                    else{
                        AnZhuangBean.listphoto.removeAt(position)
                        adapter.notifyDataSetChanged()
                    }
                    quitdialog.dismiss()
                }
//                val myAlerDialog = MyAlerDialog(this@LingShi2Activity)
//                myAlerDialog.start()
//                myAlerDialog.setdata("确定要删除这张照片？")
//                myAlerDialog.setright("取消") { myAlerDialog.getdialog().dismiss() }
//                myAlerDialog.setleft("确定",object : View.OnClickListener{
//                    override fun onClick(v: View?) {
//                        if(AnZhuangBean.listphoto[position].fid != ""){
//                            updatedialog = Myutil.showupdatedialog(this@LingShi2Activity, "正在删除照片", false)
//                            model.deleteAnZhuang(this@LingShi2Activity,oid,AnZhuangBean.listphoto[position],object : OnUserDataInterface{
//                                override fun onUserData(data: String?) {
//                                    if(data == null){
//                                        Myutil.MyTost("删除失败")
//                                        updatedialog.dismiss()
//                                        return
//                                    }
//                                    Myutil.MyTost("删除照片成功")
//                                    updatedialog.dismiss()
//                                    jsondata(data)
//                                    adapter.Allnotify(AnZhuangBean.listphoto)
//                                    Myutil.setet(AnZhuangBean.isl_asp,EtIslAsp)
//                                    Log.d("AnZhuangBean","AnZhuangBean="+AnZhuangBean.toString())
//                                }
//
//                                override fun onNot(throwable: String?) {
//                                    Log.d("getHttpClient","error"+throwable)
//                                    Myutil.MyTost("删除失败")
//                                    updatedialog.dismiss()
//                                }
//
//                            })
//
//                        }
//                        else{
//                            AnZhuangBean.listphoto.removeAt(position)
//                            adapter.notifyDataSetChanged()
//                        }
//                        myAlerDialog.getdialog().dismiss()
//                    }
//                })
//                myAlerDialog.show()
        }
        return true
    }

    override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
        if(position < AnZhuangBean.listphoto.size) {
                val intent = Intent(this@XianchangPaizhaoActivity, DanjianivActivity::class.java)
                intent.putExtra("url", AnZhuangBean.listphoto[position].uri)
                startActivity(intent)
        }else if(position == AnZhuangBean.listphoto.size){
            Myutil.photo(this@XianchangPaizhaoActivity, null, 1)
        }
    }

    public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(data==null){
            return
        }
        when (requestCode) {
            100 -> {
                val name = data.getStringExtra("dir")
                addphoto(name)
            }
        }
    }

    private fun addphoto(name: String) {
        val photo = PhotoBean()
        photo.uri = name
        AnZhuangBean.listphoto.add(photo)
        adapter.notifyDataSetChanged()
    }
}
